package com.ff.neuropp.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ff.neuropp.domain.Auth;
import com.ff.neuropp.service.AuthService;
import com.ff.neuropp.util.RequiredPermission;
import com.ff.neuropp.util.ResultTool;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 权限信息
 */
@RestController
@RequestMapping("api/auth")
@Api(value = "权限controller", tags = {"权限接口"})
public class AuthController {

    @Autowired
    private AuthService authService;


    @PostMapping("/update")
    @RequiredPermission("/api/auth/update")
    @ApiOperation(value = "更新权限信息", notes = "更新权限信息")
    public Map<String, Object> updateAuth(@Valid Auth auth) {

        authService.saveAuth(auth);

        return ResultTool.genSuccessResult("修改成功");
    }

    @PostMapping("/create")
    @RequiredPermission("/api/auth/create")
    @ApiOperation(value = "保存权限信息", notes = "保存权限信息")
    public Map<String, Object> saveAuth(@Valid Auth auth) {
        Auth check = authService.findByUrl(auth.getUrl());
        if (null != check) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "权限url已存在，不可重复");
        }
        authService.saveAuth(auth);

        return ResultTool.genSuccessResult("添加成功");
    }

    @PostMapping("/delete")
    @RequiredPermission("/api/auth/delete")
    @ApiOperation(value = "删除权限信息", notes = "删除权限信息")
    public Map<String, Object> saveHotelItem(@ApiParam(required = true, value = "权限id") @RequestParam Integer id) {
        Map<String, Object> result = new HashMap<>();

        authService.deleteById(id);

        result.put("code", 10000);
        result.put("message", "删除成功");
        return result;
    }

    @GetMapping("/findbyid")
    @ApiOperation(value = "根据id查询权限信息", notes = "根据id查询权限信息")
    public Map<String, Object> findById(@ApiParam(required = true, value = "序号") @RequestParam Integer id,
                                        HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        Auth auth = authService.findById(id);
        if (null == auth) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "权限信息不存在");
        }
        result.put("auth", auth);
        result.put("code", 10000);
        result.put("message", "查询成功");

        return result;
    }


    @GetMapping("/list")
    @ApiOperation(value = "查询权限列表", notes = "查询权限列表")
    public Map<String, Object> findList(@ApiParam(required = false, value = "关键词") @RequestParam(required = false) String tag,
                                        HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        param.put("GT_id", 0);
        if (null != tag && tag.length() > 0) {
            param.put("LIKE_name", tag);
        }
        List<Auth> list = authService.findList(param);
        if (null != list && list.size() > 0) {
            result.put("data", list);
            result.put("total", list.size());
            result.put("message", "查询成功");
            result.put("code", 10000);
        } else {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "没有查询到数据");
        }

        return result;
    }

    @GetMapping("/listpage")
    @ApiOperation(value = "查询权限分页列表", notes = "查询权限分页列表")
    public Map<String, Object> findListPage(@ApiParam(required = false, value = "权限名字 模糊搜索") @RequestParam(required = false) String authName,
                                            @ApiParam(required = false, value = "页码 1开始") @RequestParam(required = false) Integer pageNo,
                                            @ApiParam(required = false, value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize,
                                            HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        if (null == pageNo) {
            pageNo = 1;
        } else if (pageNo < 1) {
            pageNo = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }
        param.put("GT_id", 0);
        if (null != authName && authName.length() > 0) {
            param.put("LIKE_authName", authName);
        }
        Page<Auth> list = authService.findPage(pageNo, pageSize, param);
        if (null != list && list.getTotalElements() > 0) {
            result.put("data", list);
            result.put("message", "分页查询成功");
            result.put("code", 10000);
        } else {
            result.put("code", 10001);
            result.put("message", "没有查询到数据");
        }

        return result;
    }

    @GetMapping("/listauths")
    @ApiOperation(value = "查询权限列表字符串", notes = "查询权限列表字符串")
    public Map<String, Object> findStringList() {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        param.put("GT_id", 0);
        List<Auth> list = authService.findList(param);
        if (null != list && list.size() > 0) {
            List<Object> data = new ArrayList<>();
            for(Auth auth: list) {
                Map<String, Object> map = new HashMap<>();
                map.put("key", auth.getId());
                map.put("label", auth.getName());
                data.add(map);
            }
            result.put("data", data);
            //result.put("total", list.size());
            result.put("message", "查询成功");
            result.put("code", 10000);
        } else {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "没有查询到数据");
        }

        return result;
    }

}
